package cn.ekuma.epos.datalogic.define.dao.erp;

import java.util.List;

import com.openbravo.bean.PaymentsLine;
import com.openbravo.bean.erp.viewbean.FinanceChargeAnysisInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.QBFBuilder;
import com.openbravo.data.loader.SerializerRead;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.query.QBFCompareEnum;

import cn.ekuma.data.dao.ViewDAO;

public class FinanceChargeAnysisInfoDAO extends ViewDAO<FinanceChargeAnysisInfo>{

	public FinanceChargeAnysisInfoDAO(I_Session s) {
		super(s);
	}

	@Override
	public Class getSuportClass() {
		return FinanceChargeAnysisInfo.class;
	}

	@Override
	public FinanceChargeAnysisInfo readValues(DataRead dr,
			FinanceChargeAnysisInfo obj) throws BasicException {
		if(obj==null)
			obj=new FinanceChargeAnysisInfo();
		obj.setTypeId(dr.getString(1));
		obj.setDefineType(dr.getString(2));
		obj.setOrdertype(dr.getInt(3));
		obj.setUnit(dr.getDouble(4));
		obj.setTotalPrice(dr.getDouble(5));
		obj.setConsultCost(dr.getDouble(6));
		obj.setDebt(dr.getDouble(7));
		return obj;
	}
	
	public List<FinanceChargeAnysisInfo> list(Object filter) throws BasicException {
		List<FinanceChargeAnysisInfo> retList=listByFontSale(filter);
		retList.addAll(listByERP(filter));
		return retList;
	}
	
	
	public List<FinanceChargeAnysisInfo> listByFontSale(Object filter) throws BasicException {
		return new StaticSentence(s, new QBFBuilder(
                "SELECT  T.TICKETTYPE, COUNT(*), SUM(T.TOTALPRICE),SUM(T.TOTALCONSULTCOST)  "
                + "FROM TICKETS T "
                + "WHERE ?(QBF_FILTER) "
                + "GROUP BY T.TICKETTYPE", new String[]{ "T.CURDATE", "T.CURDATE", "T.LOCATIONREF"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING}), new PosFinanceChargeAnysisRead()).list(filter);
	}
	
	
    public List<FinanceChargeAnysisInfo> listByERP(Object filter) throws BasicException {
    	return new StaticSentence(s, new QBFBuilder(
                "SELECT  E.ID,E.NAME, T.ORDERTYPE, COUNT(*), SUM(T.TOTALPRICE),SUM(T.TOTALCONSULTPRICE),SUM(T.DEBT)  "
                + "FROM  ORDERS T  "
                + "LEFT OUTER JOIN ORDERTYPES E ON T.DEFINETYPEREF = E.ID "
                + "WHERE ?(QBF_FILTER) GROUP BY E.ID,E.NAME, T.ORDERTYPE ORDER BY E.ID,E.NAME, T.ORDERTYPE ", new String[]{ "T.CURDATE", "T.CURDATE", "T.LOCATIONREF"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING}), this).list(filter);

	}
    
    public static class PosFinanceChargeAnysisRead implements SerializerRead<FinanceChargeAnysisInfo>{
		@Override
		public FinanceChargeAnysisInfo readValues(DataRead dr)
				throws BasicException {
			FinanceChargeAnysisInfo	obj=new FinanceChargeAnysisInfo();
			obj.setTypeId("POS");
			obj.setDefineType("POS");
			obj.setOrdertype(dr.getInt(1));
			obj.setUnit(dr.getDouble(2));
			obj.setTotalPrice(dr.getDouble(3));
			obj.setConsultCost(dr.getDouble(4));
			obj.setDebt(0);
			return obj;
		}
    }
    
    public List<PaymentsLine> listPayments(FinanceChargeAnysisInfo info,Object filter)throws BasicException {
    	if(info.getTypeId().equalsIgnoreCase("POS"))
                return listPaymentsFont(filter,info.getOrdertype());
    	return listPaymentsERP(filter,info.getTypeId(),info.getOrdertype());
    }
    
    public List<PaymentsLine> listPaymentsFont(Object filter,int type)throws BasicException {
    	Object[] filters=(Object[])filter;
    	Object[] newfilters=new Object[filters.length+2];
    	for(int i=0;i<filters.length;i++)
    		newfilters[i]=filters[i];
    	newfilters[filters.length]=QBFCompareEnum.COMP_EQUALS;
    	newfilters[filters.length+1]=type;
    return  new StaticSentence(s            
            , new QBFBuilder("SELECT PAYMENTS.PAYMENT, SUM(PAYMENTS.TOTAL) " +
              "FROM PAYMENTS, RECEIPTS,TICKETS T " +
              "WHERE PAYMENTS.RECEIPT = RECEIPTS.ID AND RECEIPTS.ID =T.ID AND ?(QBF_FILTER) " +
              "GROUP BY PAYMENTS.PAYMENT",new String[]{ "T.CURDATE", "T.CURDATE", "T.LOCATIONREF","T.TICKETTYPE"})
            ,new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.INT})
            , new SerializerRead(){
				@Override
				public Object readValues(DataRead dr) throws BasicException {
					PaymentsLine obj=new PaymentsLine();
					obj.setM_PaymentType(dr.getString(1));
					obj.setM_PaymentValue(dr.getDouble(2));
					return obj;
				}
        	
        }).list(newfilters);
    }
    
    public List<PaymentsLine> listPaymentsERP(Object filter,String orderType,int type)throws BasicException {
    	Object[] filters=(Object[])filter;
    	Object[] newfilters=new Object[filters.length+4];
    	for(int i=0;i<filters.length;i++)
    		newfilters[i]=filters[i];
    	newfilters[filters.length]=QBFCompareEnum.COMP_EQUALS;
    	newfilters[filters.length+1]=orderType;
    	newfilters[filters.length+2]=QBFCompareEnum.COMP_EQUALS;
    	newfilters[filters.length+3]=type;
    	return  new StaticSentence(s            
                , new QBFBuilder("SELECT PAYMENTS.PAYMENT, SUM(PAYMENTS.TOTAL) " +
                  "FROM PAYMENTS, RECEIPTS,ORDERS T " +
                  "WHERE PAYMENTS.RECEIPT = RECEIPTS.ID AND RECEIPTS.ID =T.ID AND ?(QBF_FILTER) " +
                  "GROUP BY PAYMENTS.PAYMENT",new String[]{ "T.CURDATE", "T.CURDATE", "T.LOCATIONREF","T.DEFINETYPEREF","T.ORDERTYPE"})
                ,new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.INT})
                , new SerializerRead(){
    				@Override
    				public Object readValues(DataRead dr) throws BasicException {
    					PaymentsLine obj=new PaymentsLine();
    					obj.setM_PaymentType(dr.getString(1));
    					obj.setM_PaymentValue(dr.getDouble(2));
    					return obj;
    				}
            	
            }).list(newfilters);
        }
}
